SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE VIEW dbo.vw_PlazasAfectadasL
AS
SELECT ss.idsubservicio,  p.cupof AS CUPOF, 
    CASE f.regimensalarial WHEN 12 THEN NULL 
    ELSE SUM(DISTINCT sa2.horas) END AS HORAS
FROM tb_serviciosagentes sa1, tb_funciones f, 
    tb_serviciosagentes sa2, tb_plazas p, tb_plazas p2, 
    tb_subserviciosagentes ss, 
    tb_subserviciosagentes ss2
WHERE sa1.idservicioagente = ss.servicioagente AND 
    sa2.agente = sa1.agente AND p.idplaza = ss2.plaza AND 
    ss.normainicio = ss2.normainicio AND 
    ss2.servicioagente = sa2.idservicioagente AND 
    sa2.ubicacionorig = sa1.ubicacionorig AND 
    p.funcion = f.idfuncion 
GROUP BY p.cupof, ss.idsubservicio, f.regimensalarial





GO
GRANT SELECT ON  [dbo].[vw_PlazasAfectadasL] TO [SoloVer]
GO
